import privateSeaInterface from "/api/privateSea/privateSeaInterface";
import request from "/utils/request"
import { dateFormat } from "/utils/commonMethod";
//引入Promise js文件
const Promise = require('/plugins/es6-promise')
let app = getApp();

Page({
  data: {
    private: {
      total: 0,
      list: []
    },
    other: {
      total: 0,
      list: []
    },
    dataType: "private",
    dingUserTree: [],
    form: { //获取私海数据列表分页
      pageSize: 20, // 默认20条
      search: "" // 搜索门店名称、联系人
    },
    moreActionModalName: null,
    merchantId: null, // 商户id
    currrentClickIndex: null
  },
  onLoad() {
    app.getUnderUserList().then(users => {
      this.setData({
        dingUserTree: users
      })
    })
  },
  onShow() {
    // 1-重新设置页码--列表数据
    this.setData({
      "form.search": "",
      private: {
        total: 0,
        list: []
      },
      other: {
        total: 0,
        list: []
      }
    })
    //2-请求接口
    this.queryPrivateShopList({ ...this.data.form, pageNumber: 1 })
    this.queryOtherPrivateShopList({ ...this.data.form, pageNumber: 1 })
    this.hideModal();
  },
  showMoreModal(e) {
    this.setData({
      currrentClickIndex: e.currentTarget.dataset.index,
      moreActionModalName: e.currentTarget.dataset.moreModal,
      merchantId: e.currentTarget.dataset.merchantId
    })
  },
  hideModal() {
    this.setData({
      moreActionModalName: null,
      merchantId: null,
      currrentClickIndex: null
    })
  },
  // 搜索
  onSearch(e) {
    if (this.data.dataType === 'private') {
      this.setData({
        "form.search": e.detail.value,
        private: {
          total: 0,
          list: [] //清空列表数据
        }
      })
      this.queryPrivateShopList({ ...this.data.form, pageNumber: 1 })
    } else {
      this.setData({
        "form.search": e.detail.value,
        other: {
          total: 0,
          list: []
        }
      })
      this.queryOtherPrivateShopList({ ...this.data.form, pageNumber: 1 })
    }
  },
  // tab切换
  onTabSelect(e) {
    this.setData({
      "dataType": e.target.dataset.dataType,
      "form.search": ""
    })
  },
  // 移出私海
  goToPrivateSea() {
    dd.confirm({
      title: "提示",
      content: "你确定要移出私海吗?",
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      success: (result) => {
        if (result.confirm) {
          dd.showLoading({
            content: "加载中..."
          })
          request({
            url: privateSeaInterface.removePrivateShop + `?id=${this.data.merchantId}`,
            method: "post",
          }).then((res) => {
            if (res.success) {
              dd.hideLoading();
              dd.alert({
                content: res.messages[0]
              })
              //关闭所有页面 重新加载当前页面
              dd.redirectTo({
                url: "/pages/privateCustomers/private-customers-list/private-customers-list"
              });
            } else {
              dd.alert({
                content: res.messages[0]
              })
            }
          })
        }
      }
    })
  },
  addCustomer() {
    dd.navigateTo({
      url: "/pages/privateCustomers/resovle-store-link/resovle-store-link"
    })
  },
  goViewDetail() {
    dd.navigateTo({
      url: "/pages/privateCustomers/customer-details/customer-details?merchantId=" + this.data.merchantId
    })
  },
  goToColleagues() {
    dd.navigateTo({
      url: "/pages/privateCustomers/colleague-list/colleague-list?merchantId=" + this.data.merchantId
    })
  },
  // 拨打手机号
  goCallPhone(e) {
    if (e.currentTarget.dataset.phoneNumber) {
      dd.showCallMenu({
        phoneNumber: e.currentTarget.dataset.phoneNumber, // 期望拨打的电话号码
        code: '+86', // 国家代号，中国是+86
        showDingCall: true // 是否显示钉钉电话
      });
    }
  },
  // 处理下拉刷新函数
  onPullDownRefresh() {
    setTimeout(() => {
      if (this.data.dataType === 'private') {
        this.setData({
          private: {
            total: 0,
            list: []
          }
        })
        this.queryPrivateShopList({ ...this.data.form, pageNumber: 1 }).then(() => {
          dd.stopPullDownRefresh(); //停止下拉刷新
        })
      } else {
        this.setData({
          other: {
            total: 0,
            list: []
          }
        })
        this.queryOtherPrivateShopList({ ...this.data.form, pageNumber: 1 }).then(() => {
          dd.stopPullDownRefresh();
        })
      }
    }, 1000)
  },
  // 页面被拉到底部
  onReachBottom() {
    if (this.data.dataType === 'private') {
      this.data.private.list.length !== this.data.private.total && this.queryPrivateShopList({ ...this.data.form, pageNumber: this.data.private.list.length / this.data.form.pageSize + 1 })
    } else {
      this.data.other.list.length !== this.data.other.total && this.queryOtherPrivateShopList({ ...this.data.form, pageNumber: this.data.other.list.length / this.data.form.pageSize + 1 })
    }
  },
  // 查询私海数据列表
  queryPrivateShopList(params) {
    dd.showLoading({
      content: "加载中..."
    })
    if (params.pageNumber === 1) {
      this.setData({
        private: {
          list: [],
          total: 0
        }
      })
    }
    return new Promise((resolve, reject) => {
      request({
        url: privateSeaInterface.getPrivateShopList,
        method: "get",
        data: params
      }).then((res) => {
        if (res.success) {
          // 处理tags标签
          res.voList.map((item, index) => {
            item.tags = item.tags ? item.tags.split("&&") : [];
            item.lastVisitTime = dateFormat(item.lastVisitTime); // 处理日期格式展示
          })
          //分页
          if (params.pageNumber === 1) {
            this.setData({
              private: {
                total: res.totalNum,
                list: res.voList
              }
            })
          } else {
            this.setData({
              "private.list": [...this.data.private.list, ...res.voList]
            })
          }
          dd.hideLoading();
          resolve()
        }
      })
    })
  },
  // 查询他人私海数据列表
  queryOtherPrivateShopList(params) {
    dd.showLoading({
      content: "加载中..."
    })
    if (params.pageNumber === 1) {
      this.setData({
        other: {
          list: [],
          total: 0
        }
      })
    }
    return new Promise((resolve, reject) => {
      request({
        url: privateSeaInterface.getOtherPrivateShopList,
        method: "get",
        data: params
      }).then((res) => {
        if (res.success) {
          //分页
          if (params.pageNumber === 1) {
            this.setData({
              other: {
                total: res.totalNum,
                list: res.voList
              }
            })
          } else {
            this.setData({
              "other.list": [...this.data.other.list, ...res.voList]
            })
          }
          dd.hideLoading();
          resolve()
        }
      })
    })
  }
});
